package com.orhon.smartcampus.modules.systemctl.fetcher;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.modules.systemctl.dao.MenusDao;
import com.orhon.smartcampus.modules.systemctl.entity.Apis;
import com.orhon.smartcampus.modules.systemctl.entity.Menus;
import com.orhon.smartcampus.modules.systemctl.entity.Modules;
import com.orhon.smartcampus.modules.systemctl.entity.Widgets;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLApis;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLMenus;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLModule;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLWidgets;
import com.orhon.smartcampus.modules.systemctl.mm.ApisMapper;
import com.orhon.smartcampus.modules.systemctl.mm.MenuMapper;
import com.orhon.smartcampus.modules.systemctl.mm.ModuleMapper;
import com.orhon.smartcampus.modules.systemctl.mm.WidgetsMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class MenuFetcher implements DataFetcher {


	@Autowired
	MenusDao menusDao;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {

		int id = Integer.parseInt(env.getArgument("id").toString());

		String hql = "from Menus where deleted_at = null and id = "+id;
		Menus item =  menusDao.findId(hql, null);
		if(item!=null) {
			GQLMenus gqlMenus = null;
			if (env.getSelectionSet().contains("parent")){
				Menus menus = item.getMenus();
				gqlMenus = MenuMapper.MAPPER.toMenus(menus);
			}
			GQLModule gqlModule = null;
			if (env.getSelectionSet().contains("module")){
				Modules modules = item.getModules();
				gqlModule = ModuleMapper.MAPPER.mapModule(modules);
			}

			List<GQLWidgets> GQLWidgets= null;
			if (env.getSelectionSet().contains("widgets")){
				List<Widgets> widgets = item.getWidgets();
				GQLWidgets = WidgetsMapper.MAPPER.toWidgetsList(widgets);
			}

			List<GQLMenus> GQLMenus= null;
			if (env.getSelectionSet().contains("childreen")){
				List<Menus> lists = item.getChildreen();
				GQLMenus = MenuMapper.MAPPER.toMenusList(lists);
			}

			List<GQLApis> GQLApis= null;
			if (env.getSelectionSet().contains("operationApiRelations")){
				List<Apis> lists = item.getApis();
				GQLApis = ApisMapper.MAPPER.toApisList(lists);
			}

			GQLMenus menus = MenuMapper.MAPPER.toMenus(item);
			menus.setParent(gqlMenus);
			menus.setModule(gqlModule);
			menus.setWidgets(GQLWidgets);
			menus.setChildreen(GQLMenus);
			menus.setApis(GQLApis);

			return menus;
		}
		return null;


	}



}
